#include <windows.h>
#include "Utils.h"
#include <string>
Ogre::Vector3 fixCoordSystem(Ogre::Vector3 v)
{
    return Ogre::Vector3(v.x, v.z, -v.y);
}

Ogre::Vector3 fixCoordSystem2(Ogre::Vector3 v)
{
    return Ogre::Vector3(v.x, v.z, v.y);
}

Ogre::Quaternion fixCoordSystemQuat(Ogre::Quaternion v)
{
    return Ogre::Quaternion(v.w, -v.x, -v.z, v.y);
}

float frand()
{
    return rand()/(float)RAND_MAX;
}

float randfloat(float lower, float upper)
{
    return lower + (upper-lower)*(rand()/(float)RAND_MAX);
}

int randint(int lower, int upper)
{
    return lower + (int)((upper+1-lower)*frand());
}

void fixnamen(char *name, size_t len)
{
    for (size_t i=0; i<len; i++) {
        if (i>0 && name[i]>='A' && name[i]<='Z' && isalpha(name[i-1])) {
            name[i] |= 0x20;
        } else if ((i==0 || !isalpha(name[i-1])) && name[i]>='a' && name[i]<='z') {
            name[i] &= ~0x20;
        }
    }
}

void fixname(std::string &name)
{
    for (size_t i=0; i<name.length(); i++) {
        if (i>0 && name[i]>='A' && name[i]<='Z' && isalpha(name[i-1])) {
            name[i] |= 0x20;
        } else if ((i==0 || !isalpha(name[i-1])) && name[i]>='a' && name[i]<='z') {
            name[i] &= ~0x20;
        }
    }
}

FILE *flog;
bool glogfirst = true;

void gLog(char *str, ...)
{
    if (glogfirst) {
        flog = fopen("log.txt","w");
        fclose(flog);
        glogfirst = false;
    }

    flog = fopen("log.txt","a");

    va_list ap;

    va_start (ap, str);
    vfprintf (flog, str, ap);
    va_end (ap);

    fclose(flog);
}


int indexMapBuf(int x, int y)
{
    return ((y+1)/2)*9 + (y/2)*8 + x;
}

void BuildRoughIndices(unsigned short *pIndices, unsigned int nGrideSize)
{
    //WORD wBase = 0;
    //WORD wMid = (nGrideSize + 1) * (nGrideSize + 1);
    //WORD *pIndex = pIndices;
    //for(UINT j = 0; j < nGrideSize; j++)
    //{
    //    for(WORD wEnd = wBase + nGrideSize; wEnd > wBase; wBase++, wMid++)
    //    {
    //        *pIndex++ = wBase;
    //        *pIndex++ = wBase + nGrideSize + 1 ;
    //        *pIndex++ = wMid;

    //        *pIndex++ = wBase;
    //        *pIndex++ = wMid ;
    //        *pIndex++ = wBase + 1 ;

    //        *pIndex++ = wBase + 1;
    //        *pIndex++ = wMid ;
    //        *pIndex++ = wBase + nGrideSize + 2 ;

    //        *pIndex++ = wBase + nGrideSize + 2;
    //        *pIndex++ = wMid;
    //        *pIndex++ = wBase + nGrideSize + 1 ;
    //    }
    //    wBase++;
    //}
    unsigned short *defstrip = new unsigned short[stripsize];
    for (int i=0; i<stripsize; i++) 
        defstrip[i] = i; // note: this is ugly and should be handled in stripify
    stripify<unsigned short>(defstrip, pIndices);
    delete[] defstrip;

}

void BuildDetailIndices(unsigned short *pIndices, unsigned int nGrideSize)
{
    //WORD *pIndex = pIndices;
    //WORD wBase = 0;
    //for(UINT j = 0; j < nGrideSize; j++)
    //{
    //    WORD wNextLine = wBase + nGrideSize + 1;
    //    while(wBase < wNextLine)
    //    {
    //        *pIndex++ = wBase;
    //        *pIndex++ = wBase + nGrideSize + 1;
    //        wBase++;
    //    }
    //    if(j < nGrideSize - 1)
    //    {
    //        pIndex[0] = pIndex[-1];
    //        pIndex[1] = wNextLine;
    //        pIndex += 2;
    //    }
    //}
    unsigned short *defstrip = new unsigned short[stripsize2];
    for (int i=0; i<stripsize2; i++) 
        defstrip[i] = i; // note: this is ugly and should be handled in stripify
    stripify2<unsigned short>(defstrip, pIndices);
    delete[] defstrip;
}